<?php
namespace Admin\Controller;
use Think\Controller;
// 继承公共的控制器
class PlatformController extends CommonController {

   public function _initialize(){
       parent::_initialize();
       // $access=\Home\Common\checkpower(9);
       // if($access!==0){
       //     $this->error('权限不够');
       // }
      }

   public function index(){
   	$type = M('platform');
   	$data = $type->order('id')->select();
   	
   	$this->assign("data", $data);
   	$this->display();
   }


   public function add(){
   	$this->display();
   }

   //启用相关栏目，如果启用底层栏目，也需要启用其直接的父级目录(如果被禁用的话)
   public function active_doit(){
      $type   = M('platform');
      $cateId = I('get.id'); //获取cateId

      $data['id']      = $cateId;
      $data['status']  = 1;
      if($type->create($data)) {
         $res = $type->save();
         if($res) {
            $this->success('启用成功',U('Platform/index'));
         } else {
            $this->error('启用失败',U('Platform/index'));
         }
      }
   }

   /**
    * 添加分类
    */
   public function add_doit(){
   		//获得传递过来的分类名称
   		$type = M('platform');
   		
   		$data['name']           = I('name');
   		$data['referrer_code']  = I('referrer_code', '');

   		if($type->create($data)){
   			$res = $type->add();
   			if(false !== $res){
          $this->success('新增成功','index');
        }else{
          $this->error('添加分类失败','index');
        }
   		}
   }


   //删除对应的分类
   function delete_doit() {
   	  $type   = M('platform');
      $cateId = I('id', 0);

      $data['id']     = $cateId;
      $data['status'] = 0;
   	  $res = $type->save($data);

     	if($res){
     		$this->success('禁用成功',U('Platform/index'));
     	} else {
     		$this->error('禁用失败',U('Platform/index'));
     	}
   }


  /**
   * 修改平台相关信息
   */
   public function update(){
   	$type = M('platform');
   	$id   = I('id');
   	$val  = $type->find($id);

  	$this->assign('val',$val);
    $this->display('update');
   }


   /**
    * 更新平台相关信息
    * @return [type] [description]
    */
   function update_doit(){
   	$type = M('platform');
   	$data = $type->create();
   	if($data){
   		$res = $type->save($data);
   		if(false !==$res){
   			$this->success('更新成功',U('Platform/index'));
   		}else{
   			$this->error('更新失败',U('Platform/index'));
   		}
   	}
   }
}